package logic

import (
	"context"
	"errors"
	"wei/models/mysql"

	"wei/weirpc/internal/svc"
	"wei/weirpc/weirpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdatePasswordLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdatePasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdatePasswordLogic {
	return &UpdatePasswordLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdatePasswordLogic) UpdatePassword(in *weirpc.UpdatePasswordRequest) (*weirpc.UpdatePasswordResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	mysql.DB.Debug().Where("account=?", in.Account).Updates(&user)
	if in.Account != user.Account {
		return nil, errors.New("用户不存在,不允许修改")
	}

	if in.Password != in.NewPassword {
		return nil, errors.New("两次密码必须一致")
	}

	if in.Password == user.Password {
		return nil, errors.New("新密码与旧密码一致,不允许修改")
	}

	return &weirpc.UpdatePasswordResponse{
		UserId: int64(user.ID),
	}, nil
}
